package com.example.Handler;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.examplee.username.httpfrist_01.R;

public class HandlerMessageActivity extends AppCompatActivity {
TextView textView;
    class person{
        String name;
        String address;
        int age;

        @Override
        public String toString() {
            return "姓名："+name+"地址："+address+"年龄:"+age;
        }
    }
    Handler mHandler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
//          textView.setText(""+msg.arg1+"-"+msg.arg2);


            textView.setText(" "+msg.obj);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_message);
        textView= (TextView) findViewById(R.id.text_handler1);
        new Thread(){

            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    Message message=mHandler.obtainMessage();
//                    message.arg1=88;
//                    message.arg2=200;
                    person per=new person();
                    per.age=23;
                    per.address="四川";
                    per.name="李四";
                    message.obj=per;
                    message.sendToTarget();//调用的是一个Handler的message方法
//                   mHandler.sendMessage(message);
//                     message.sendToTarget();与 mHandler.sendMessage(message);是一样的
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
